package alkaid.oss.local.domain;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import alkaid.oss.common.exceptions.EntityMissingException;
import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;

/**
 * 文件元数据服务
 *  
 * @author Lucky Yang
 * @since 0.0.1
 */
@Service
@RequiredArgsConstructor
public class FileMetadataService {
    private final FileMetadataRepository fileMetadataRepository;
 
    public FileMetadata save(String name, String path, String contentType, long size) {
        Assert.notBlank(path);

        FileMetadata fmd = new FileMetadata(name, path, contentType, size);
        return fileMetadataRepository.save(fmd);
    }

    @Transactional(rollbackFor = Exception.class)
    public FileMetadata updateName(Long id, String newName) {      
        FileMetadata fmd = this.findById(id);
        fmd.setName(newName);
        return fileMetadataRepository.save(fmd);
    }

    public FileMetadata findById(Long id) {
        return fileMetadataRepository.findById(id).orElseThrow(EntityMissingException::new);
    } 
}
